package com.oscar.ehome.base.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.oscar.ehome.presenter.BasePresenter;
import com.oscar.ehome.utils.view.LoadingDialog;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;

/**
 * Created by on 2017/5/19 11:56.
 *
 * @author mengwanhe
 */

public abstract class BaseFragment<V, T extends BasePresenter> extends Fragment {

    private boolean flag;
    //加载框
    private LoadingDialog loadingDialog;
    //无数据时
    ProgressFrameLayout frameLayout;
    protected T mPresenter;//Presenter对象


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = createPresenter();//创建Presenter
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }
    }

    protected void showLoading() {
        showLoading(false);
        flag = false;
    }

    protected void showLoading(boolean canceledOnTouchOutside) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(getActivity());
        }
        if (!loadingDialog.isShowing()) {
            loadingDialog.show(canceledOnTouchOutside);
        }
    }

    protected void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    protected void showLayout(int isNetWork) {
        if (frameLayout == null) {
            frameLayout = new ProgressFrameLayout(getActivity());
            frameLayout.setLayout(isNetWork);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    protected abstract T createPresenter();

}
